<template>

    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 商品管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
      <div class="container" :style="isPc ?'padding:30px': 'padding:10px'">

            <div class="handle-box">
                <el-button type="primary" icon="el-icon-add" @click="editVisible = true">新增代理</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column v-if="isPc" prop="flag"  label="标签"></el-table-column>
                <el-table-column prop="user_id" label="用户Id"   align="center"></el-table-column>
                <el-table-column prop="ratio"  label="比例(%)"></el-table-column>
                <el-table-column label="修改"  align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            style="padding: 2px 0;"
                            @click="handleEdit(scope.$index, scope.row)"
                        >修改
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="基本信息" :visible.sync="editVisible" :width="modalWidth"  :close-on-click-modal="false">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="标签">
                    <el-input v-model="form.flag"></el-input>
                </el-form-item>

                <el-form-item label="用户Id">
                    <el-input v-model="form.user_id"></el-input>
                </el-form-item>
                <el-form-item label="比例">
                    <el-input v-model="form.ratio"></el-input>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>

    </div>
</template>

<script>

export default {
    name: 'agent',
    data() {
        return {
            query: {
                name: '',
            },
            tableData: [],
            form: {
                id: "",
                flag: "",
                user_id: "",
                ratio: "",
            },
            editVisible: false,
            modalWidth : '80%',
            isPc: false
        };
    },
    components: {

    },
    created() {
        if (document.body.clientWidth > 768) {
          this.modalWidth = '40%'
          this.isPc = true
        }
        this.getData();
    },
    methods: {
        // 获取 easy-mock 的模拟数据
        getData() {
            this.http.getAgent(this.query).then(res => {
                this.tableData = res.data;
            });
        },
        saveEdit(){
            this.http.saveAgent(this.form).then(res => {
                if(res.code === 0) {
                    this.$message.success("修改成功")
                }
            })
            this.editVisible = false;
            this.getData()
        },
        // 编辑操作
        handleEdit(index, row) {
            this.form = row;
            this.editVisible = true;
        },
    }
};
</script>

<style scoped>
.el-row {
    margin-bottom: 20px;
    background-color: #fff;
}
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.add-dialog-input {
    width: 80%;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
